package com.aoxin.controller.opm;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aoxin.model.dto.TreeDto;
import com.aoxin.model.opm.SlfOrg;
import com.aoxin.service.inter.opm.OrgService;
import com.aoxin.util.AjaxRes;
import com.aoxin.util.Const;
import com.aoxin.util.ThreadLocalUtil;
import com.aoxin.util.enumerate.OperateType;
import com.aoxin.util.log.LogInfo;

/**
 * 组织机构Controller
 * 
 * @author Melody
 * @date 2018年3月20日 下午5:50:15
 */
@Controller
@ResponseBody
@RequestMapping("/web/org")
public class OrgController {

	@Autowired
	private OrgService orgService;

	/**
	 * 添加机构
	 * 
	 * @param org
	 *            机构
	 */
	@RequestMapping("/insertOrg")
	@LogInfo(content = "组织机构",operateType = OperateType.INSERT)
	public AjaxRes insertOrg(SlfOrg org, AjaxRes ajaxRes) {
		try {
			this.orgService.insertSlfOrg(org);
			ajaxRes.setSucceedMsg(Const.SAVE_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 删除机构
	 * 
	 * @param orgId
	 *            机构ID
	 */
	@RequestMapping("/deleteOrg")
	@LogInfo(content = "组织机构",operateType = OperateType.DELETE)
	public AjaxRes deleteOrg(@RequestParam("orgId") Integer orgId, AjaxRes ajaxRes) {
		try {
			this.orgService.deleteSlfOrg(orgId);
			ajaxRes.setSucceedMsg(Const.DEL_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 批量删除机构
	 * 
	 * @param orgIds
	 *            机构ID集合
	 */
	@RequestMapping("/deleteOrgs")
	@LogInfo(content = "组织机构",operateType = OperateType.DELETE)
	public AjaxRes deleteOrgs(@RequestParam("orgIds") List<Integer> orgIds, AjaxRes ajaxRes) {
		try {
			this.orgService.deleteSlfOrgByIds(orgIds);
			ajaxRes.setSucceedMsg(Const.DEL_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 修改机构
	 * 
	 * @param orgIds
	 *            机构ID集合
	 */
	@RequestMapping("/updateOrg")
	@LogInfo(content = "组织机构",operateType = OperateType.UPDATE)
	public AjaxRes updateOrg(SlfOrg org, AjaxRes ajaxRes) {
		try {
			this.orgService.updateSlfOrg(org);
			ajaxRes.setSucceedMsg(Const.UPDATE_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 修改机构状态
	 * 
	 * @param orgIds
	 *            机构ID集合
	 */
	@RequestMapping("/updateOrgStates")
	@LogInfo(content = "组织机构",operateType = OperateType.UPDATE)
	public AjaxRes updateOrgStates(@RequestParam("orgIds") List<Integer> orgIds, @RequestParam("state") Integer state, AjaxRes ajaxRes) {
		try {
			this.orgService.updateSlfOrgStates(orgIds, state);
			ajaxRes.setSucceedMsg(Const.UPDATE_SUCCEED);

		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 根据机构ID查询机构
	 * 
	 * @param orgId
	 *            机构ID
	 * @return 机构
	 */
	@RequestMapping("/getOrg")
	@LogInfo(content = "组织机构",operateType = OperateType.QUERY)
	public AjaxRes getOrg(@RequestParam("orgId") Integer orgId, AjaxRes ajaxRes) {
		try {
			SlfOrg slfOrg = this.orgService.getSlfOrg(orgId);
			ajaxRes.setSucceed(slfOrg, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 查询机构
	 * 
	 * @param map
	 *            查询条件
	 * @return 机构集合
	 */
	/*@RequestMapping("/getSlfOrgList")
	@LogInfo(content = "组织机构",operateType = OperateType.QUERY)
	public AjaxRes getSlfOrgList(@RequestParam Map<String, Object> map, AjaxRes ajaxRes) {
		try {
			List<SlfOrg> slfOrgs = this.orgService.getSlfOrgList(map);
			ajaxRes.setSucceed(slfOrgs, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;

	}*/

	/**
	 * 分页查询机构
	 * 
	 * @param map
	 *            条件集合
	 * @return
	 */
	@RequestMapping("/slicedQueryOrg")
	@LogInfo(content = "组织机构",operateType = OperateType.QUERY)
	public AjaxRes slicedQueryOrg( Integer orgId, Integer state, AjaxRes ajaxRes) {
		try {
			List<SlfOrg> slfOrgs = this.orgService.getSlfOrgList(orgId,state);
			ajaxRes.setSucceed(slfOrgs, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 获取下一排序号
	 * 
	 * @param partId
	 *            父ID
	 * @return 排序号
	 */
	@RequestMapping("/getNextSequence")
	@LogInfo(content = "组织机构",operateType = OperateType.QUERY)
	public AjaxRes getNextSequence(@RequestParam(value = "partId", required = false) Integer partId, AjaxRes ajaxRes) {
		try {
			Long sequence = this.orgService.getNextSequence(partId);
			ajaxRes.setSucceed(sequence, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 查询组织机构树
	 * 
	 * @return 机构树
	 */
	@RequestMapping("/getOrgTreeList")
	@LogInfo(content = "组织结构树", operateType = OperateType.QUERY)
	public AjaxRes getOrgTreeList(AjaxRes ajaxRes) {
		try {
			List<SlfOrg> treeDtos = this.orgService.getOrgTreeList(ThreadLocalUtil.getEmployee().getOrgId());
			ajaxRes.setSucceed(treeDtos, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
}
